package com.lixin.smartclassroom.ui.home.tab2.activity

import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.addpingjia
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.bean.EvaluationQuestionsDto
import com.lixin.smartclassroom.common.recyclerDialog
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivitySetEvaluateQuestionBinding
import com.lixin.smartclassroom.ui.home.adapter.evaluateQuestionWeiduList
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast

/**
 * Description
 * Author: lwc
 * Date:2024/2/6 14:19
 */
class SetEvaluateQuestionAct : BaseActivity<ActivitySetEvaluateQuestionBinding>() {
    override fun getViewBinding() = ActivitySetEvaluateQuestionBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<EvaluationQuestionsDto>
    override fun title() = "设置题目"
    private var questionType = ""
    private var smallid = ""
    private var questionScore = 0
    override fun textMenuContent() = "完成"

    override fun onTextMenuListener() {
        super.onTextMenuListener()
        addQuestionData()
        saveSuccessDialog("添加完成")
        finish()
    }

    private fun addQuestionData() {
        binding.apply {
            scopeNetLife {
                if (etTitle.text().isEmpty()) {
                    toast("请输入问题")
                    return@scopeNetLife
                }
                val baseBean = addpingjia(listBean, smallid, etTitle.text(), etAnalysis.text())
                if (baseBean.result == "0") {
                    listBean.clear()
                    binding.recycler.bindingAdapter.notifyDataSetChanged()
                    etTitle.setText("")
                    etAnalysis.setText("")
                }
            }
        }
    }

    override fun iniView() {
        smallid = getIntentString("smallid")
        listBean = ArrayList()
        listBean.add(EvaluationQuestionsDto("", "0", ""))
        binding.recycler.evaluateQuestionWeiduList().apply {
            R.id.tvWeiDu.onClick {
                recyclerDialog(9, "选择关联维度", "取消", "确定", currentPosition = modelPosition)
            }

            R.id.btnPlus.onClick {
                listBean[modelPosition].nums =
                    listBean[modelPosition].nums?.toInt()?.plus(1).toString()
                binding.recycler.bindingAdapter.notifyDataSetChanged()
            }

            R.id.btnMinus.onClick {
                if (listBean[modelPosition].nums?.toInt()!! > 0) {
                    listBean[modelPosition].nums =
                        listBean[modelPosition].nums?.toInt()?.minus(1).toString()
                    listBean[modelPosition].nums =
                        listBean[modelPosition].nums?.toInt()?.minus(1).toString()
                } else toast("分值不能小于0")
            }
        }.models = listBean

        receiveEvent<DataListBean>("weiduEvaluate") {
            listBean[it.selectPosition?.toInt()!!].name = it.id.toString()
            listBean[it.selectPosition?.toInt()!!].weiduname = it.name.toString()
            binding.recycler.bindingAdapter.notifyDataSetChanged()
        }

    }

    override fun onClick() {
        binding.apply {
            btnNext.onClick {
                addQuestionData()
            }

            btnMore.onClick {
                listBean.add(EvaluationQuestionsDto("", "0", ""))
                binding.recycler.bindingAdapter.notifyDataSetChanged()

            }
        }
    }


}